/**
 * 
 */
package com.yufei.toolinterface.util;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hamcrest.core.IsInstanceOf;

import com.yufei.infoExtractor.core.CollectionElementType;
import com.yufei.infoExtractor.util.CommonUtil;
import com.yufei.infoExtractor.util.ExceptionUtil;
import com.yufei.toolinterface.EditTextField;

/**
 * @author zhaoyufei zhao-0244@qq.com
 *
 * created at 2012-9-5 下午5:25:03
 */
public class DialogUtil {
	public static final int DialogHeight = 400;
	public static final int DialogWidth = 300;
	static Log mLog = LogFactory.getLog(DialogUtil.class);

public static void fullScreen(Component component){
	component.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
	
}

public static void popUpDialog(String message){
	JOptionPane.showMessageDialog(null, message, null,  JOptionPane.ERROR_MESSAGE);

}
public static JDialog CreateDialogForCustomerObj(JDialog parent, boolean modal,final Object entity){
	JDialog dialog=new JDialog(parent, modal);
	dialog.setTitle(entity.getClass().getSimpleName());
	
	dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
	java.awt.GridLayout gridLayout = new GridLayout(0, 4, 50, 50);
      
	final Container contentPane = dialog.getContentPane();
	contentPane.setLayout(gridLayout);
	//将属性组装到面板上
	List<Field> fields=CommonUtil.getAllDeclareFields(entity.getClass());
	Object value=null;
	Class fieldCla=null;
	for(Field field:fields){
		try {
			fieldCla=PropertyUtils.getPropertyType(entity, field.getName());
			value=PropertyUtils.getProperty(entity, field.getName());
		} catch (Exception e) {ExceptionUtil.getExceptionDetailsMessage(e);}

		if(!CommonUtil.isCustomerObjType(fieldCla)){
			//collection
			if(CommonUtil.isCollection(fieldCla)){
				contentPane.add(new JLabel(field.getName()));
				 Class subElementType = null;
					try {
						CollectionElementType  annotation= CommonUtil.getFieldByFieldName(entity.getClass(), field.getName()).getAnnotation(CollectionElementType.class);;
								//elementType.getField(jTable1.getColumnName(col)).getAnnotation(CollectionElementType.class);
						subElementType =annotation.type();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						mLog.info(ExceptionUtil.getExceptionDetailsMessage(e));
					} 
				contentPane.add(new ListButton(value, field.getName(), dialog,subElementType) );
			}
			//common java object type can dispaly and theire value
			else{
				contentPane.add(new JLabel(field.getName()));
				try {
				    EditTextField comp = new EditTextField(entity, field.getName());
					if(value!=null){
				 comp.setText((String)value);
					contentPane.add(comp);//new JTextField(value.toString()));
					}
					else{
					     contentPane.add(new JTextField());

					}
				} catch (Exception e) {ExceptionUtil.getExceptionDetailsMessage(e);}
			}
		}
		
		//用户自定义类型
		else{
			contentPane.add(new JLabel(field.getName()));
			contentPane.add(new EntityButton(value, "实体信息", dialog));
		}
		
	}
	 JButton saveButton=new JButton();
	    saveButton.setSize(20, 50);
	    saveButton.setText("Save");
	    saveButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
	/*			String fieldName=null;
				Object value=null;
				Component[] comps=contentPane.getComponents();
				for(Component comp:comps){
					if(comp instanceof EditTextField ){
						fieldName=((EditTextField)comp).getFieldName();
						value=((EditTextField) comp).getText();
						try {
							PropertyUtils.setProperty(entity, fieldName, value);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							mLog.info(ExceptionUtil.getExceptionDetailsMessage(e1));
						}

					}
				}*/
				
			}
		});
	    contentPane.add(saveButton);
	return dialog;
	
}
public static JDialog createDialogForCollectionInformation(final JFrame parent, boolean modal,final Collection collection,final Class elementType) {
	final JDialog dialog=new JDialog(parent, modal);
	 dialog.setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
	dialog.setTitle("集合信息显示");
	JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
	jScrollPane1.setSize(DialogWidth, DialogHeight);
   
	try {
		 final JTable jTable1 = ComponentFactory.createJTableForCollectionInformation(dialog, collection, elementType);
		 
		 
		 
		 
		 jScrollPane1.setViewportView(jTable1);
		    
		    
		    
		    
		    
		    
		    dialog.add(jScrollPane1);
		  
		    ButtonJPanel  buttonJPanel=new ButtonJPanel();
		    JButton saveButton=buttonJPanel.getSaveButton();
		    JButton deleteButton=buttonJPanel.getDeleteButton();
		    JButton addButton=buttonJPanel.getAddButton();
		   



		    //dialog.add(saveButton);
		  //  dialog.add(deleteButton);
		    dialog.add(buttonJPanel);

		    dialog.setModal(modal);
		    saveButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					Collection collection1=null;
					collection1=TableModelUtil.convertVectorToEntity(elementType,(DefaultTableModel)jTable1.getModel());
					collection.removeAll(collection);
			        collection.addAll(collection1);
					DialogUtil.popUpDialog("保存成功！");
				}
			});
		    deleteButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					
					int[] selectedRows=jTable1.getSelectedRows();
					for(int selectedRow:selectedRows){
						  ((DefaultTableModel)jTable1.getModel()).removeRow(selectedRow);

					}
					Collection collection1=null;
					collection1=TableModelUtil.convertVectorToEntity(elementType,(DefaultTableModel)jTable1.getModel());
					collection.removeAll(collection);
			        collection.addAll(collection1);
					DialogUtil.popUpDialog("删除成功！");
				}
			});
		    addButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					TableModel model = jTable1.getModel();
					try {
						((DefaultTableModel) model).addRow(TableModelUtil.convertEntityToVector(elementType.newInstance(), (AbstractTableModel) model));
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}


					
				}
				
			});

	} catch (Exception e1) {
		// TODO Auto-generated catch block
  mLog.info(ExceptionUtil.getExceptionDetailsMessage(e1));
	} 

    		


   
   // dialog.setLocationRelativeTo(dialog);

   // dialog.setVisible(true);

	return dialog;
}
public static JDialog CreateDialogForCustomerObj(JFrame parent, boolean modal,Object entity){
	JDialog dialog=new JDialog(parent, modal);
	dialog.setTitle(entity.getClass().getSimpleName());
	
	dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
	java.awt.GridLayout gridLayout = new GridLayout(0, 4, 50, 50);
      
	final Container contentPane = dialog.getContentPane();
	contentPane.setLayout(gridLayout);
	//将属性组装到面板上
	List<Field> fields=CommonUtil.getAllDeclareFields(entity.getClass());
	Object value=null;
	Class fieldCla=null;
	for(Field field:fields){
		try {
			fieldCla=PropertyUtils.getPropertyType(entity, field.getName());
			value=PropertyUtils.getProperty(entity, field.getName());
		} catch (Exception e) {ExceptionUtil.getExceptionDetailsMessage(e);}

		if(!CommonUtil.isCustomerObjType(fieldCla)){
			//collection
			if(CommonUtil.isCollection(fieldCla)){
				contentPane.add(new JLabel(field.getName()));
				 Class subElementType = null;
					try {
						CollectionElementType  annotation= CommonUtil.getFieldByFieldName(entity.getClass(), field.getName()).getAnnotation(CollectionElementType.class);;
								//elementType.getField(jTable1.getColumnName(col)).getAnnotation(CollectionElementType.class);
						subElementType =annotation.type();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						mLog.info(ExceptionUtil.getExceptionDetailsMessage(e));
					} 
				contentPane.add(new ListButton(value, field.getName(), dialog,subElementType) );
			}
			//common java object type can dispaly and theire value
			else{
				contentPane.add(new JLabel(field.getName()));
				try {
				    EditTextField comp = new EditTextField(entity, field.getName());
					if(value!=null){
				 comp.setText((String)value);
					contentPane.add(comp);//new JTextField(value.toString()));
					}
					else{
					     contentPane.add(new JTextField());

					}
				} catch (Exception e) {ExceptionUtil.getExceptionDetailsMessage(e);}
			}
		}
		
		//用户自定义类型
		else{
			contentPane.add(new JLabel(field.getName()));
			contentPane.add(new EntityButton(value, "实体信息", dialog));
		}
		
	}
	 JButton saveButton=new JButton();
	    saveButton.setSize(20, 50);
	    saveButton.setText("Save");
	    saveButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
	/*			String fieldName=null;
				Object value=null;
				Component[] comps=contentPane.getComponents();
				for(Component comp:comps){
					if(comp instanceof EditTextField ){
						fieldName=((EditTextField)comp).getFieldName();
						value=((EditTextField) comp).getText();
						try {
							PropertyUtils.setProperty(entity, fieldName, value);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							mLog.info(ExceptionUtil.getExceptionDetailsMessage(e1));
						}

					}
				}*/
				
			}
		});
	    contentPane.add(saveButton);
	return dialog;
	
}
}
